package com.snapp.web.actions.object;

import javax.servlet.http.HttpServletRequest;

import org.jdom.JDOMException;

import com.snapp.util.object.model.ObjectModel;
import com.snapp.web.SNAppAccess;
import com.snapp.web.agents.ObjectAgent;

public class Save extends com.snapp.web.actions.secure.Save {

	public String execute(HttpServletRequest req) {
		if (SNAppAccess.isSecure(req)) {
			
			String sgn = req.getParameter("saveGlobalId");
			if (sgn == null) {
				setResults(RESULT_CODE_ERROR, "Missing required parameter:  \"saveGlobalId\"");
				return snappResponse(req);
			}
			
			ObjectAgent oa = new ObjectAgent();
			ObjectModel om = oa.saveObject(sgn, consolidateWriteMap(req));
			
			try {
				setResultCode(RESULT_CODE_SUCCESS);
				setResultMessage("Successfully saved object.");
				setBodyElement(om.getBean().toXML());
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				log("Error building response", e);
			}
		}
		return snappResponse(req);
	}
}
